package com.renjun.smweb.service.TestService.impl;

import com.alibaba.fastjson.JSONObject;
import com.renjun.smweb.commons.componet.BaseDaoComponent;
import com.renjun.smweb.commons.util.CollectionUtils;
import com.renjun.smweb.commons.util.ResponseUtils;
import com.renjun.smweb.dao.bean.*;
import com.renjun.smweb.dao.pojo.LoginInfo;
import com.renjun.smweb.service.TestService.TestService;
import com.renjun.smweb.commons.componet.BaseDaoComponent;
import org.apache.log4j.Logger;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

/**
 * Created by huamin on 2017/2/15.
 */
@Service
public class TestServiceImpl implements TestService {
    @Autowired
    private BaseDaoComponent baseDaoComponent;

    private Logger log = Logger.getLogger(TestServiceImpl.class);

    @Override
    public JSONObject testQuery(Map<String,Object> params) throws SQLException {
        LoginInfo loginInfo = (LoginInfo) params.remove("userinfo");
        String id= (String) params.get("id");
        QueryParams queryParams = QueryParams.createQueryParams("t_msg_task");
        queryParams.addQueryParamsByMap(CollectionUtils.removeEmptyKeyByParams(params));
//        try {
//            queryParams.setDbLog(loginInfo);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
        queryParams.printSelf();
        List<Map<String, Object>> results = baseDaoComponent.selectDataByParams(queryParams);
        return ResponseUtils.createSuccessResponseBody(results);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public JSONObject testAdd(Map<String,Object> params) throws SQLException {
        LoginInfo loginInfo = (LoginInfo) params.remove("userinfo");
        String id= (String) params.get("id");
        InsertParams ins = InsertParams.createInsertParams("T_MENUINFO","id");
        ins.setValues(id);
        log.debug("\n ceshiadd---------------------");
//        try {
//            ins.setDbLog(loginInfo);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
        ins.printSelf();
        baseDaoComponent.insertDataByParams(ins);
        InsertParams ins2 = InsertParams.createInsertParams("T_MENUINFO","id");
        ins2.setValues("bbb");
        baseDaoComponent.insertDataByParams(ins2);
        return ResponseUtils.createSuccessResponseBody("success");
    }

    @Override
    public JSONObject testUpdate(Map<String,Object> params) throws SQLException {
        LoginInfo loginInfo = (LoginInfo) params.remove("userinfo");
        String id= (String) params.get("id");
        UpdateParams updateParams = UpdateParams.createUpdateParams("t_msg_task");
        updateParams.addWhereParameter(Parameter.createParameter("id",id));
        updateParams.addParam(Parameter.createParameter("id", "bbb"));
        log.debug("\n ceshiupdate---------------------");
//        try {
//            updateParams.setDbLog(loginInfo);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
        updateParams.printSelf();
        baseDaoComponent.updateDataByParams(updateParams);
        return ResponseUtils.createSuccessResponseBody("success");
    }

    @Override
    public JSONObject testDelete(Map<String,Object> params) throws SQLException {
        LoginInfo loginInfo = (LoginInfo) params.remove("userinfo");
        String id= (String) params.get("id");
        DeleteParams deleteParams = DeleteParams.createDeleteParams("t_msg_task", Parameter.createParameter("id", id));
        baseDaoComponent.deleteDataByParams(deleteParams);
//        try {
//            deleteParams.setDbLog(loginInfo);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
        deleteParams.printSelf();
        baseDaoComponent.deleteDataByParams(deleteParams);
        return ResponseUtils.createSuccessResponseBody("success");
    }

}
